package com.xm.servlet;

import com.xm.biz.*;
import com.xm.biz.impl.*;
import com.xm.po.*;
import com.xm.util.AlipayUtil;
import com.xm.util.RandomUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author xie'lin'feng
 * @date 2020-08-24-11:47
 * @description 提交订单处理器
 */
@WebServlet("/buyCommit")
public class BuyOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        CartBiz cartBiz = new CartBizImpl();
        GoodsBiz goodsBiz = new GoodsBizImpl();
        OrderBiz orderBiz = new OrderBizImpl();
        OrderGoodsBiz orderGoodsBiz = new OrderGoodsBizImpl();
        OrderSettlementsBiz orderSettlementsBiz = new OrderSettlementsBizImpl();
        //获取地址id
        Integer addressId = Integer.valueOf(req.getParameter("addressId"));
        HttpSession session = req.getSession();
        //获取用户
        Users users = (Users) session.getAttribute("user");
        //计算总金额
        double total = 0.0;
        for (Cart cart : cartBiz.getCartByIdChecked(users.getUserId())) {
            total += cart.getGoodsCnt()*goodsBiz.getGoodsByGoodsId(cart.getGoodsId()).getShopPrice();
        }
        //新增订单
        Order order = new Order();
        String randomCode = RandomUtil.RandomCode(10);
        //订单编号 随机数加时间戳
        order.setOrderNo(randomCode+System.currentTimeMillis());
        //订单状态 默认未支付订单
        order.setOrderStatus(-2);
        //商品总金额
        order.setTotalMoney(total);
        //是否支付 默认未支付
        order.setIsPay(0);
        //配送方式 默认商城配送
        order.setDeliverType(0);
        //收货地址
        order.setUserAddressId(addressId);
        //下单时间
        order.setCreateTime(new Date());
        //支付来源
        order.setPayFrom(1);
        //订单备注
        order.setOrderRemarks("无备注");
        boolean result = orderBiz.add(order);
        if (result){
            Order orderByNo = orderBiz.getOrderByNo(order.getOrderNo());
            for (Cart cart : cartBiz.getCartByIdChecked(users.getUserId())) {
                OrderGoods orderGoods = new OrderGoods();
                //订单编号
                orderGoods.setOrderId(orderByNo.getOrderId());
                //商品编号
                orderGoods.setGoodsId(cart.getGoodsId());
                //商品数量
                orderGoods.setGoodsNums(cart.getGoodsCnt());
                //商品价格
                orderGoods.setGoodsPrice(goodsBiz.getGoodsByGoodsId(cart.getGoodsId()).getShopPrice());
                //商品略缩图
                orderGoods.setGoodsThums(goodsBiz.getGoodsByGoodsId(cart.getGoodsId()).getGoodsThums());
                //商品名称
                orderGoods.setGoodsName(goodsBiz.getGoodsByGoodsId(cart.getGoodsId()).getGoodsName());
                //创建时间
                orderGoods.setCreateTime(new Date());
                result = orderGoodsBiz.add(orderGoods);
            }
            if (result){
                OrderSettlements osm = new OrderSettlements();
                //订单编号
                osm.setOrderId(orderByNo.getOrderId());
                //结算订单编号
                osm.setSettlementNo("Settlement"+System.currentTimeMillis());
                //创建时间
                osm.setCreateTime(new Date());
                //结算类型
                osm.setSettlementType(0);
                //订单实际金额
                osm.setOrderMoney(total);
                //结算金额
                osm.setSettlementMoney(0.0);
                //结算备注
                osm.setRemarks("无备注");
                //是否完成结算
                osm.setIsFinish(0);
                result = orderSettlementsBiz.add(osm);
            }
        }
        if (result){
            for (Cart cart : cartBiz.getCartByIdChecked(users.getUserId())) {
                result = cartBiz.delete(cart.getGoodsId(),users.getUserId());
            }
            if (result){
                PrintWriter out = resp.getWriter();
                out.print(AlipayUtil.pay(order.getOrderNo(),total));
                out.flush();
                out.close();
            }
        }
    }
}
